var express = require("express");
var router = express.Router();
var db = require("../model/index");
const date = require('../public/package')


/* 修改某个管理员的信息*/
router.post("/home/accout", function (req, res, next) {
    let mysql = `UPDATE  admin SET username = ?,phone=?,email=? ,name=? WHERE id = ?`;
    db(
        mysql,
        [
            req.body.username,
            req.body.phone,
            req.body.email,
            req.body.name,
            req.body.id,
            // req.body.roleid,
        ],
        function (err, data) {
            if (err) {
                return res.json({
                    code: 500,
                    msg: "修改管理员信息失败，请稍后重试",
                    data: err,
                });
            }
            res.json({
                code: 200,
                msg: "修改管理员信息成功",
                data: data,
            });
        }
    );
});
// 获取首页销售额的数据
router.get("/home/system", function (req, res, next) {
    let HeadData = [];
    let today = date.dateFn().split(' ')[0];
    let yesterday = new Date();
    let sevenday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    sevenday.setDate(sevenday.getDate() - 7);
    yesterday = JSON.stringify(yesterday).substring(1, 11);
    let oneday = yesterday + ' ' + '23:59:59';
    sevenday = JSON.stringify(sevenday).substring(1, 11) + ' ' + '00:00:00';
    let mysql = `SELECT * FROM orders`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询首页头部订单列表失败，请稍后重试",
            });
        };
        let Neworders = 0;
        let Newtotal = 0;
        let Oldtotal = 0;
        let Seventotal = 0;
        data.filter((item) => {
            let orderTime = JSON.stringify(item.handletime).substring(1, 11);
            if (orderTime == today) {
                Newtotal += item.money;
                Neworders++;
            } else if (Date.parse(item.handletime) <= Date.parse(oneday) && Date.parse(item.handletime) >= Date.parse(sevenday)) {
                if (orderTime == yesterday) {
                    Oldtotal += item.money;
                } else {
                    Seventotal += item.money;
                }
            }
        });
        let obj = {};
        obj.Newtotal = Newtotal;
        obj.Neworders = Neworders;
        obj.Oldtotal = Oldtotal;
        obj.Seventotal = Seventotal + Oldtotal;
        HeadData.push(obj)
        console.log(HeadData)
        res.json({
            code: 200,
            msg: "查询首页头部订单列表成功",
            data: HeadData,
        });
    });
});
// 获取订单数据表的数据
router.get("/home/system/things", function (req, res, next) {
    let arr = [];
    let obj = {
        Issued: 0,
        Return: 0,
        payment: 0,
        replacement: 0,
        complete: 0,
        delivery: 0
    };
    let mysql = `SELECT * FROM orders`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询首页订单数据列表失败，请稍后重试",
            });
        }
        data.filter((item) => {
            switch (item.type) {
                case '待付款':
                    obj.Issued += 1;
                    break;
                case '待发货':
                    obj.Return += 1;
                    break;
                case '待确认退货':
                    obj.payment += 1;
                    break;
                case '待处理换货':
                    obj.replacement += 1;
                    break;
                case '已完成':
                    obj.complete += 1;
                    break;
                case '已发货':
                    obj.delivery += 1;
                    break;
            }
        })
        arr.push(obj)
        res.json({
            code: 200,
            msg: "查询首页订单数据列表成功",
            data: arr,
        });
    });
});
// 获取商品数量
router.get("/home/system/goods", function (req, res, next) {
    let arr = [];
    let obj = {
        been_on: 0,
        been_off: 0,
        nervous: 0,
        all_goods: 0
    };
    let mysql = `SELECT * FROM goods`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询数据列表失败，请稍后重试",
            });
        }
        data.filter((item) => {
            if (item.shelftype == '是') {
                obj.been_on += 1;
            } else {
                obj.been_off += 1;
            };
        });
        obj.all_goods = data.length;
        arr.push(obj)
        res.json({
            code: 200,
            msg: "查询数据列表成功",
            data: arr,
        });
    });
});
// 获取用户数量
router.get("/home/system/users", function (req, res, next) {
    let arr = [];
    let obj = {
        newUser: 0,
        oldUser: 0,
        newMonth: 0,
        allmembers: 0,
    };
    let today = date.dateFn().split(' ')[0];
    let yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    yesterday = JSON.stringify(yesterday).substring(1, 11);
    //获取本月第一天时间戳
    let firstDay = new Date();
    firstDay = new Date(firstDay.getFullYear(), firstDay.getMonth(), 2);
    firstDay = Date.parse(JSON.stringify(firstDay).substring(1, 11) + ' ' + '00:00:00');
    //获取本月最后一天时间戳
    let lastDay = new Date();
    lastDay = new Date(lastDay.getFullYear(), lastDay.getMonth() + 1, 1);
    lastDay = Date.parse(JSON.stringify(lastDay).substring(1, 11) + ' ' + '23:59:59');
    let mysql1 = `SELECT * FROM user`;
    db(mysql1, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询数据列表失败，请稍后重试",
            });
        }
        data.filter((item) => {
            item.vip == '是' ? obj.allmembers += 1 : obj.allmembers = obj.allmembers;
            if (JSON.stringify(item.addtime).substring(1, 11) == today) {
                obj.newUser++;
            } else if (JSON.stringify(item.addtime).substring(1, 11) == yesterday) {
                obj.oldUser++;
            };
            if (Date.parse(item.addtime) >= firstDay && Date.parse(item.addtime) <= lastDay) {
                obj.newMonth++;
            }

        });
        arr.push(obj)
        res.json({
            code: 200,
            msg: "查询数据列表成功",
            data: arr,
        });
    });
});
module.exports = router;